package com.esint;

import redis.clients.jedis.Jedis;

/**
 * @description: redis List类型测试类
 * @author: 谷明
 * @createDate: 2021/8/12 0:32
 */
public class TestList {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.43.166", 6379);
        jedis.flushDB();

        System.out.println("======================添加一个List======================");
        jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeekHashMap", "LinkedList");
        jedis.lpush("collections","HashSet");
        jedis.lpush("collections","HashMap");
        jedis.lpush("collections","TreeSet");
        jedis.lpush("collections","HashMap");
        jedis.lpush("collections","TreeMap");
        System.out.println("collections的内容：" + jedis.lrange("collections", 0, -1));
        System.out.println("collections区间0-3的内容：" + jedis.lrange("collections", 0, 3));
        System.out.println("============================================");

        //删除列表指定的值，第二个参数为删除的个数（在元素有重复的情况下），后add进去的会先被删除，从左到右删
        System.out.println("删除指定元素个数："+jedis.lrem("collections", 2, "HashMap"));
        System.out.println("删除后查看collections的内容：" + jedis.lrange("collections", 0, -1));
        System.out.println("删除除了指定区间（0-3）之外的所有元素：" + jedis.ltrim("collections", 0, 3));
        System.out.println("删除后查看collections的内容：" + jedis.lrange("collections", 0, -1));
        System.out.println("collections列表出栈（左端）：" + jedis.lpop("collections"));
        System.out.println("collections列表出栈（左端）后查看collections的内容：" + jedis.lrange("collections", 0, -1));
        System.out.println("从列表右端添加元素，与lpush对应：" + jedis.rpush("collections", "RightHashMap"));
        System.out.println("从列表右端添加元素后查看collections的内容：" + jedis.lrange("collections", 0, -1));
        System.out.println("collections列表出栈（右端）：" + jedis.rpop("collections"));
        System.out.println("collections列表出栈（右端）后查看collections的内容：" + jedis.lrange("collections", 0, -1));
        System.out.println("修改指定下标1的内容：" + jedis.lset("collections", 1, "Index1ValueChanged"));
        System.out.println("修改指定下标1的内容后查看collections的内容：" + jedis.lrange("collections", 0, -1));

        System.out.println("============================================");
        System.out.println("collections当前长度：" + jedis.llen("collections"));
        System.out.println("获取下标为2的元素：" + jedis.lindex("collections",2));
        System.out.println("============================================");

        System.out.println("新增一个list："+jedis.lpush("sortedList","3","13","6","99","23","13","44","78","9"));
        System.out.println("sortedList排序前：" + jedis.lrange("sortedList", 0, -1));
        System.out.println("执行排序：" + jedis.sort("sortedList"));
        System.out.println("sortedList排序后：" + jedis.lrange("sortedList", 0, -1));


    }
}
